<?php 

	// 奖品配置
	$prize_arr = array(
		    '0' => array('id'=>1,'min'=>135,'max'=>189,'prize'=>'一等奖','v'=>10), 

    		'1' => array('id'=>2,'min'=>225,'max'=>289,'prize'=>'二等奖','v'=>10),

    		'2' => array('id'=>3,'min'=>1,'max'=>44,'prize'=>'三等奖','v'=>10), 
    		
    		'3' => array('id'=>4,'min'=>array(45,90,180,270,315), 'max'=>array(89,134,224,314,360),'prize'=>'再接再励','v'=>70)
		);



	foreach ($prize_arr as $key => $val) { 
	    $arr[$val['id']] = $val['v']; 
	} 

	$prize_id = get_prize_id($arr); //根据概率获取奖项id 
	 
	$prize = $prize_arr[$prize_id-1]; //中奖项 
	$min = $prize['min']; 
	$max = $prize['max']; 
	if($prize['id']==4){ //四等奖 
	    $i = mt_rand(0,4); 
	    $result['angle'] = mt_rand($min[$i],$max[$i]); 
	}else{ 
	    $result['angle'] = mt_rand($min,$max); //随机生成一个角度 
	} 
	$result['prize'] = $prize['prize']; 
	echo json_encode($result); 



	/**
	 * 通过概率返回奖品索引
	 * @param  [type] $prize_arr
	 * 		   array('id' => id, 'v' => 占比)
	 * @return int 
	 */
	function get_prize_id($prize_arr) { 
    	$result = ''; 
    	//概率数组的总概率精度 
    	$total_probability = array_sum($prize_arr); 
	    //概率数组循环 
	    foreach ($prize_arr as $key => $val) { 
	    	//从1-100 获得一个随机数
	        $rand_num = mt_rand(1, $total_probability); 
	        if ($rand_num <= $val) { 
	            $result = $key; 
	            break; 
	        } else { 
	            $total_probability -= $val; 
	        } 
	    } 
	    unset ($prize_arr); 
	    return $result; 
	} 	


?>